package com.example.teamtribe.main.chat.chatting;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.teamtribe.R;
import com.example.teamtribe.base.BaseActivity;
import com.example.teamtribe.database.Chatting;
import com.example.teamtribe.database.ChattingBox;
import com.example.teamtribe.database.User;
import com.example.teamtribe.utils.Utility;

import org.litepal.crud.DataSupport;

import java.sql.Date;

import butterknife.BindView;

public class ChatActivity extends BaseActivity implements View.OnClickListener, TextWatcher {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv_msg)
    RecyclerView rvMsg;
    @BindView(R.id.et_text)
    EditText etText;
    @BindView(R.id.bt_send)
    Button btSend;
    private ChattingAdapter adapter;
    private User user;
    private ChattingBox chattingBox;
    //private List<Chatting> chattingList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        user = getIntent().getParcelableExtra("user");
        tvTitle.setText(user.getNickname());
        chattingBox = DataSupport.where("userid = ?", user.getRealId() + "").limit(1).find(ChattingBox.class, true).get(0);
        if (chattingBox == null) {
            ChattingBox last = DataSupport.findLast(ChattingBox.class);
            chattingBox = new ChattingBox(last.getRealId() + 1, user.getRealId());
        }
        adapter = new ChattingAdapter(chattingBox);
        rvMsg.setLayoutManager(new LinearLayoutManager(this));
        rvMsg.setAdapter(adapter);
        rvMsg.scrollToPosition(chattingBox.getChattingList().size() - 1);
        etText.addTextChangedListener(this);
        btSend.setOnClickListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (etText.getText().length() > 0) {
            btSend.setBackground(getDrawable(R.drawable.button_stroke));
            btSend.setTextColor(getResources().getColor(R.color.colorAccent));
            btSend.setEnabled(true);
        } else {
            btSend.setBackground(getDrawable(R.drawable.button_stroke_unable));
            btSend.setTextColor(getResources().getColor(R.color.unable));
            btSend.setEnabled(false);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_send:
                Chatting last = DataSupport.findLast(Chatting.class);
                chattingBox.add(new Chatting(last.getRealId() + 1, Utility.getUser().getRealId(), new Date(System.currentTimeMillis()), etText.getText().toString()));
                notifyItemChanged();
                etText.getText().clear();
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        chattingBox.saveOrUpdate();
    }

    private void notifyItemChanged() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyItemInserted(chattingBox.getChattingList().size());
                adapter.notifyItemRangeChanged(chattingBox.getChattingList().size(),1);
                rvMsg.scrollToPosition(chattingBox.getChattingList().size() - 1);
            }
        });
    }
}
